global input "Q:\dc1prhcmsas01\PU2\data - sas"
global temp "Q:\dc1prhcmsas01\PU2\temp_stata_wr"
global log "Q:\dc1prhcmsas01\PU2\Log - Stata WR"
global output "Q:\dc1prhcmsas01\PU2\data_stata_wr"
global graphs "Q:\dc1prhcmsas01\PU2\graphs_stata_wr"
global tables "Q:\dc1prhcmsas01\PU2\tables_stata_wr"

cd "Q:\dc1prhcmsas01\PU2\Code - Stata WR"
set scheme plotplain
graph set window fontface "Times New Roman"

capture log close
log using "$log/7_table_reg_growth", replace

*Program to estimate regressions
cap program drop save_est
program define save_est 
args name start

	*Save estimates
	use  "$output/`name'_table_estimates.dta", clear
	
	local index = `start'
	forvalues i=1/2 {
		preserve 
		keep if _n==`i'
		
		*Regression Coefficients
		sum beta1
		global b`index'_t0=round(r(mean), 0.0001)
		sum se1 
		global se`index'_t0=round(r(mean),0.0001)
		sum beta2
		global b`index'_t1=round(r(mean), 0.0001)
		sum se2 
		global se`index'_t1=round(r(mean),0.0001)
		sum beta3
		global b`index'_t2=round(r(mean), 0.0001)
		sum se3 
		global se`index'_t2=round(r(mean),0.0001)
		sum beta_change
		global b`index'_change=round(r(mean), 0.0001)
		sum se_change
		global se`index'_change=round(r(mean),0.0001)
		
		*Baseline characteristics
		sum n
		global n`index'=string(r(mean), "%10.0fc")
		
		restore 
		local ++index
	}
end 


*Table I
save_est fig5b_log_base_apr 1
save_est fig5b_log_base 3

	capture erase "$tables/table1.tex"
	file open fh3 using "$tables/table1.tex", write replace

	file write fh3 ///
		"\begin{tabular}{@{\hskip\tabcolsep\extracolsep\fill} l cc cc cc cc}" _n ///
		"\toprule" _n ///
		"&& (1) && (2) && (3) && (4) \\" _n ///
		"\midrule" _n ///
		"Pre-trend && $b1_t0 && $b2_t0 && $b3_t0 && $b4_t0  \\" _n ///
		" && ($se1_t0) && ($se2_t0) && ($se3_t0) && ($se4_t0) \\" _n ///
		"[1em] \\" _n ///
		"Anticipation && $b1_t1 && $b2_t1 && $b3_t1 && $b4_t1  \\" _n ///
		" && ($se1_t1) && ($se2_t1) && ($se3_t1) && ($se4_t1) \\" _n ///
		"[1em] \\" _n ///
		"Post-trend && $b1_t2 && $b2_t2 && $b3_t2 && $b4_t2  \\" _n ///
		" && ($se1_t2) && ($se2_t2) && ($se3_t2) && ($se4_t2) \\" _n ///
		"[1em] \\" _n ///
		"Change in Slope && $b1_change && $b2_change && $b3_change && $b4_change \\" _n ///
		" && ($se1_change) && ($se2_change) && ($se3_change) && ($se4_change) \\" _n ///
		"\midrule" _n ///
		"Worker FE 		  && Y && Y && Y && Y  \\" _n ///
		"Month FE 		  && Y && Y && Y && Y  \\" _n ///
		"Firm-Month FE 	  && - && Y && - && Y  \\" _n ///
		"N && $n1 && $n2 && $n3 && $n4  \\" _n ///
		"Sample	  && Apr && Apr  &&  Dec  && Dec   \\" _n ///
		"\bottomrule" _n ///
		"\end{tabular}" _n
		
	file close fh3 
	macro drop fh3 
	
*Table I
save_est fig5b_log_base_apr 1
save_est fig5b_log_base 3
save_est fig5d_log_base 5

	capture erase "$tables/table1_wide.tex"
	file open fh3 using "$tables/table1_wide.tex", write replace

	file write fh3 ///
		"\begin{tabular}{@{\hskip\tabcolsep\extracolsep\fill} l cc cc cc cc cc cc}" _n ///
		"\toprule" _n ///
		"&& (1) && (2) && (3) && (4) && (5) && (6) \\" _n ///
		"\midrule" _n ///
		"Pre-trend && $b1_t0 && $b2_t0 && $b3_t0 && $b4_t0 && $b5_t0 && $b6_t0  \\" _n ///
		" && ($se1_t0) && ($se2_t0) && ($se3_t0) && ($se4_t0) && ($se5_t0) && ($se6_t0) \\" _n ///
		"[1em] \\" _n ///
		"Anticipation && $b1_t1 && $b2_t1 && $b3_t1 && $b4_t1 && $b5_t1 && $b6_t1  \\" _n ///
		" && ($se1_t1) && ($se2_t1) && ($se3_t1) && ($se4_t1) && ($se5_t1) && ($se6_t1) \\" _n ///
		"[1em] \\" _n ///
		"Post-trend && $b1_t2 && $b2_t2 && $b3_t2 && $b4_t2 && $b5_t2 && $b6_t2  \\" _n ///
		" && ($se1_t2) && ($se2_t2) && ($se3_t2) && ($se4_t2) && ($se5_t2) && ($se6_t2) \\" _n ///
		"[1em] \\" _n ///
		"Change in Slope && $b1_change && $b2_change && $b3_change && $b4_change && $b5_change && $b6_change \\" _n ///
		" && ($se1_change) && ($se2_change) && ($se3_change) && ($se4_change) && ($se5_change) && ($se6_change) \\" _n ///
		"\midrule" _n ///
		"Worker FE 		  && Y && Y && Y && Y && Y && Y  \\" _n ///
		"Month FE 		  && Y && Y && Y && Y && Y && Y  \\" _n ///
		"Firm-Month FE 	  && - && Y && - && Y && - && Y  \\" _n ///
		"N && $n1 && $n2 && $n3 && $n4 && $n5 && $n6 \\" _n ///
		"Sample	  && Apr && Apr  &&  Dec  && Dec  &&  Apr+Dec && Apr+Dec \\" _n ///
		"\bottomrule" _n ///
		"\end{tabular}" _n
		
	file close fh3 
	macro drop fh3 

*Table I - Log
save_est fig5b_log_base 1
save_est fig5c_log_base 3
save_est fig5d_log_base 5

	capture erase "$tables/table1_log.tex"
	file open fh3 using "$tables/table1_log.tex", write replace

	file write fh3 ///
		"\begin{tabular}{@{\hskip\tabcolsep\extracolsep\fill} l cc cc cc cc cc cc}" _n ///
		"\toprule" _n ///
		"&& (1) && (2) && (3) && (4) && (5) && (6) \\" _n ///
		"\midrule" _n ///
		"Pre-trend && $b1_t0 && $b2_t0 && $b3_t0 && $b4_t0 && $b5_t0 && $b6_t0  \\" _n ///
		" && ($se1_t0) && ($se2_t0) && ($se3_t0) && ($se4_t0) && ($se5_t0) && ($se6_t0) \\" _n ///
		"[1em] \\" _n ///
		"Anticipation && $b1_t1 && $b2_t1 && $b3_t1 && $b4_t1 && $b5_t1 && $b6_t1  \\" _n ///
		" && ($se1_t1) && ($se2_t1) && ($se3_t1) && ($se4_t1) && ($se5_t1) && ($se6_t1) \\" _n ///
		"[1em] \\" _n ///
		"Post-trend && $b1_t2 && $b2_t2 && $b3_t2 && $b4_t2 && $b5_t2 && $b6_t2  \\" _n ///
		" && ($se1_t2) && ($se2_t2) && ($se3_t2) && ($se4_t2) && ($se5_t2) && ($se6_t2) \\" _n ///
		"[1em] \\" _n ///
		"Change in Slope && $b1_change && $b2_change && $b3_change && $b4_change && $b5_change && $b6_change \\" _n ///
		" && ($se1_change) && ($se2_change) && ($se3_change) && ($se4_change) && ($se5_change) && ($se6_change) \\" _n ///
		"\midrule" _n ///
		"Worker FE 		  && Y && Y && Y && Y && Y && Y  \\" _n ///
		"Month FE 		  && Y && - && Y && - && Y && -  \\" _n ///
		"Firm-Month FE 	  && - && Y && - && Y && - && Y  \\" _n ///
		"N && $n1 && $n2 && $n3 && $n4 && $n5 && $n6 \\" _n ///
		"Control Group	  && \$953 && \$953  &&  Y2014  && Y2014  &&  \$913 Apr2016 && \$913 Apr2016 \\" _n ///
		"\bottomrule" _n ///
		"\end{tabular}" _n
		
	file close fh3 
	macro drop fh3 
	
	
*Table I Apr
save_est fig5b_log_base_apr 1
save_est fig5c_log_base_apr 3

	capture erase "$tables/table1_log_apr.tex"
	file open fh3 using "$tables/table1_log_apr.tex", write replace

	file write fh3 ///
		"\begin{tabular}{@{\hskip\tabcolsep\extracolsep\fill} l cc cc cc cc}" _n ///
		"\toprule" _n ///
		"&& (1) && (2) && (3) && (4)  \\" _n ///
		"\midrule" _n ///
		"Pre-trend && $b1_t0 && $b2_t0 && $b3_t0 && $b4_t0  \\" _n ///
		" && ($se1_t0) && ($se2_t0) && ($se3_t0) && ($se4_t0) \\" _n ///
		"[1em] \\" _n ///
		"Anticipation && $b1_t1 && $b2_t1 && $b3_t1 && $b4_t1  \\" _n ///
		" && ($se1_t1) && ($se2_t1) && ($se3_t1) && ($se4_t1) \\" _n ///
		"[1em] \\" _n ///
		"Post-trend && $b1_t2 && $b2_t2 && $b3_t2 && $b4_t2  \\" _n ///
		" && ($se1_t2) && ($se2_t2) && ($se3_t2) && ($se4_t2)\\" _n ///
		"[1em] \\" _n ///
		"Change in Slope && $b1_change && $b2_change && $b3_change && $b4_change \\" _n ///
		" && ($se1_change) && ($se2_change) && ($se3_change) && ($se4_change) \\" _n ///
		"\midrule" _n ///
		"Outcome 		  && Log Base && Log Base && Log Base && Log Base && \\" _n ///
		"Worker FE 		  && Y && Y && Y && Y  \\" _n ///
		"Month FE 		  && Y && - && Y && -  \\" _n ///
		"Firm-Month FE 	  && - && Y && - && Y  \\" _n ///
		"N && $n1 && $n2 && $n3 && $n4 \\" _n ///
		"Control Group	  && >\$913 && >\$913  &&  Y2014  && Y2014  \\" _n ///
		"\bottomrule" _n ///
		"\end{tabular}" _n
		
	file close fh3 
	macro drop fh3 
	

	

*Program to estimate regressions
cap program drop save_est
program define save_est 
args name start

	*Save estimates
	use  "$output/`name'_table_estimates.dta", clear
	
	local index = `start'
	forvalues i=1/2 {
		preserve 
		keep if _n==`i'
		
		*Regression Coefficients
		sum beta1
		global b`index'_t0=round(r(mean), 0.001)
		sum se1 
		global se`index'_t0=round(r(mean),0.001)
		sum beta2
		global b`index'_t1=round(r(mean), 0.001)
		sum se2 
		global se`index'_t1=round(r(mean),0.001)
		sum beta3
		global b`index'_t2=round(r(mean), 0.001)
		sum se3 
		global se`index'_t2=round(r(mean),0.001)
		sum beta_change
		global b`index'_change=round(r(mean), 0.001)
		sum se_change
		global se`index'_change=round(r(mean),0.001)
		
		*Baseline characteristics
		sum n
		global n`index'=string(r(mean), "%10.0fc")
		
		restore 
		local ++index
	}
end 


	
	
*Table I - Dec 2016
save_est fig5b_base 1
save_est fig5c_base 3
save_est fig5d_base 5

	capture erase "$tables/table1_dec.tex"
	file open fh3 using "$tables/table1_dec.tex", write replace

	file write fh3 ///
		"\begin{tabular}{@{\hskip\tabcolsep\extracolsep\fill} l cc cc cc cc cc cc}" _n ///
		"\toprule" _n ///
		"&& (1) && (2) && (3) && (4) && (5) && (6) \\" _n ///
		"\midrule" _n ///
		"Pre-trend && $b1_t0 && $b2_t0 && $b3_t0 && $b4_t0 && $b5_t0 && $b6_t0  \\" _n ///
		" && ($se1_t0) && ($se2_t0) && ($se3_t0) && ($se4_t0) && ($se5_t0) && ($se6_t0) \\" _n ///
		"[1em] \\" _n ///
		"Anticipation && $b1_t1 && $b2_t1 && $b3_t1 && $b4_t1 && $b5_t1 && $b6_t1  \\" _n ///
		" && ($se1_t1) && ($se2_t1) && ($se3_t1) && ($se4_t1) && ($se5_t1) && ($se6_t1) \\" _n ///
		"[1em] \\" _n ///
		"Post-trend && $b1_t2 && $b2_t2 && $b3_t2 && $b4_t2 && $b5_t2 && $b6_t2  \\" _n ///
		" && ($se1_t2) && ($se2_t2) && ($se3_t2) && ($se4_t2) && ($se5_t2) && ($se6_t2) \\" _n ///
		"[1em] \\" _n ///
		"Change in Slope && $b1_change && $b2_change && $b3_change && $b4_change && $b5_change && $b6_change \\" _n ///
		" && ($se1_change) && ($se2_change) && ($se3_change) && ($se4_change) && ($se5_change) && ($se6_change) \\" _n ///
		"\midrule" _n ///
		"Worker FE 		  && Y && Y && Y && Y && Y && Y  \\" _n ///
		"Month FE 		  && Y && - && Y && - && Y && -  \\" _n ///
		"Firm-Month FE 	  && - && Y && - && Y && - && Y  \\" _n ///
		"N && $n1 && $n2 && $n3 && $n4 && $n5 && $n6 \\" _n ///
		"Control Group	  && \$953 && \$953  &&  Y2014  && Y2014  &&  \$913 Apr2016 && \$913 Apr2016 \\" _n ///
		"\bottomrule" _n ///
		"\end{tabular}" _n
		
	file close fh3 
	macro drop fh3 

*Table I Apr
save_est fig5b_base_apr 1
save_est fig5c_base_apr 3

	capture erase "$tables/table1_apr.tex"
	file open fh3 using "$tables/table1_apr.tex", write replace

	file write fh3 ///
		"\begin{tabular}{@{\hskip\tabcolsep\extracolsep\fill} l cc cc cc cc}" _n ///
		"\toprule" _n ///
		"&& (1) && (2) && (3) && (4)  \\" _n ///
		"\midrule" _n ///
		"Pre-trend && $b1_t0 && $b2_t0 && $b3_t0 && $b4_t0  \\" _n ///
		" && ($se1_t0) && ($se2_t0) && ($se3_t0) && ($se4_t0) \\" _n ///
		"[1em] \\" _n ///
		"Anticipation && $b1_t1 && $b2_t1 && $b3_t1 && $b4_t1  \\" _n ///
		" && ($se1_t1) && ($se2_t1) && ($se3_t1) && ($se4_t1) \\" _n ///
		"[1em] \\" _n ///
		"Post-trend && $b1_t2 && $b2_t2 && $b3_t2 && $b4_t2  \\" _n ///
		" && ($se1_t2) && ($se2_t2) && ($se3_t2) && ($se4_t2)\\" _n ///
		"[1em] \\" _n ///
		"Change in Slope && $b1_change && $b2_change && $b3_change && $b4_change \\" _n ///
		" && ($se1_change) && ($se2_change) && ($se3_change) && ($se4_change) \\" _n ///
		"\midrule" _n ///
		"Outcome 		  && Base && Base && Base && Base && \\" _n ///
		"Worker FE 		  && Y && Y && Y && Y  \\" _n ///
		"Month FE 		  && Y && - && Y && -  \\" _n ///
		"Firm-Month FE 	  && - && Y && - && Y  \\" _n ///
		"N && $n1 && $n2 && $n3 && $n4 \\" _n ///
		"Control Group	  && >\$913 && >\$913  &&  Y2014  && Y2014  \\" _n ///
		"\bottomrule" _n ///
		"\end{tabular}" _n
		
	file close fh3 
	macro drop fh3 
	

	
	
log close

/*
	file write fh3 ///
		"\begin{tabular}{@{\hskip\tabcolsep\extracolsep\fill} l cc cc cc cc}" _n ///
		"\toprule" _n ///
		"&& (1) && (2) && (3) && (4) \\" _n ///
		"\midrule" _n ///
		"Pre-trend && $b1_t0 && $b2_t0 && $b3_t0 && $b4_t0 \\" _n ///
		" && ($se1_t0) && ($se2_t0) && ($se3_t0) && ($se4_t0) \\" _n ///
		"[1em] \\" _n ///
		"Anticipation && $b1_t1 && $b2_t1 && $b3_t1 && $b4_t1  \\" _n ///
		" && ($se1_t1) && ($se2_t1) && ($se3_t1) && ($se4_t1) \\" _n ///
		"[1em] \\" _n ///
		"Post-trend && $b1_t2 && $b2_t2 && $b3_t2 && $b4_t2 \\" _n ///
		" && ($se1_t2) && ($se2_t2) && ($se3_t2) && ($se4_t2) \\" _n ///
		"[1em] \\" _n ///
		"Change in Slope && $b1_change && $b2_change && $b3_change && $b4_change \\" _n ///
		" && ($se1_change) && ($se2_change) && ($se3_change) && ($se4_change)\\" _n ///
		"\midrule" _n ///
		"Worker FE 		  && Y && Y && Y && Y  \\" _n ///
		"Month FE 		  && Y && - && Y && -  \\" _n ///
		"Firm-Month FE 	  && - && Y && - && Y  \\" _n ///
		"N && $n1 && $n2 && $n3 && $n4 \\" _n ///
		"Control Group	  && >\$913 && >\$913  &&  Y2014  && Y2014  \\" _n ///
		"\bottomrule" _n ///
		"\end{tabular}" _n
		
	file close fh3 
	macro drop fh3 